package com.bootemp.control;

import com.bootemp.bean.Dept;
import com.bootemp.bean.Emp;
import com.bootemp.service.DeptService;
import com.bootemp.service.EmpService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jdk.internal.org.objectweb.asm.tree.analysis.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "emp")
public class EmpControl {
    @Resource
    public EmpService es;
    @PostMapping(value = "/insert")
    public int insert(Emp e, MultipartFile pic){
        //得到文件名
        String fname=pic.getOriginalFilename();
        if (fname!=null&&!fname.equals("")){
            //封装
            e.setEpic(fname);
            try {
                e.setEpic2(pic.getInputStream());
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            //调用服务层的插入方法
            return es.insert(e);
        }
        return 0;
    }
    @GetMapping(value = "/all")
    public List<Emp> all(HttpServletRequest req){
        //服务器路径
        String path=req.getServletContext().getRealPath("upload");
        File f=new File(path);
        if (!f.exists())
            f.mkdir();
        //调用所有
        return  es.all(path);

    }
    @GetMapping(value = "/allemp")
    public List<Emp> allemp(HttpServletRequest req){
        //服务器路径
        String path=req.getServletContext().getRealPath("upload");
        File f=new File(path);
        if (!f.exists())
            f.mkdir();
        //调用所有
        return  es.allEmp(path);

    }
    @GetMapping(value = "/alldept")
    public List<Dept> alldept(HttpServletRequest req){
        //服务器路径
        String path=req.getServletContext().getRealPath("upload");
        File f=new File(path);
        if (!f.exists())
            f.mkdir();
        //调用所有
        return  es.allDept(path);

    }
    @GetMapping(value = "/page")
    public PageInfo<Emp> page(int cur,HttpServletRequest req){
        //服务器路径
        String path=req.getServletContext().getRealPath("upload");
        File f=new File(path);
        if (!f.exists())
            f.mkdir();
        //设置当前页及页长度
        PageHelper.startPage(cur,5);
        List<Emp> data=es.all(path);
        return new PageInfo<>(data);
    }
    @DeleteMapping(value = "/delete")
    public int delete(int id){
        return  es.delete(id);
    }
    @Resource
    public DeptService ds;
    /**
     *
     * @param id
     * @return  包含一个员工信息及所有的部门信息
     */
    @GetMapping(value = "/one")
    public Map<String,Object> one(int id){
        //员工信息
        Emp one=es.one(id);
        //所有部门
        List<Dept> list=ds.all();
        Map<String,Object> data=new HashMap<>();
        data.put("one",one);
        data.put("list",list);
        return  data;
    }
    @PostMapping(value = "/update")
    public int update(Emp e, MultipartFile pic){
        //得到文件名
        String fname=pic.getOriginalFilename();
        if (fname!=null&&!fname.equals("")){
            //封装
            e.setEpic(fname);
            try {
                e.setEpic2(pic.getInputStream());
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            //调用服务层的插入方法
            return es.update(e);
        }
        return es.update2(e);
    }
    //练习：工资控制层
}
